<?php
namespace app\models;
use ac\component\tool\Tool;
use app\models\Basic;

use Yii;

class Poster extends Basic
{
    
    public static function tableName()
    {
        return '{{%poster}}';
    }

    public function rules()
    {
        return [
            ['name', 'required', 'message' => 'name不能为空', 'on' => 'add'],
            ['name', 'string', 'message' => 'name必须是字符串',
                                'min' => 1, 'tooShort' => 'name最短1位',
                                'max' => 16, 'tooLong' => 'name最长16位'],
            ['location', 'required', 'message' => 'location不能为空', 'on' => 'add'],
            ['location', 'string', 'message' => 'location必须是字符串',
                                'min' => 1, 'tooShort' => 'location最短1位',
                                'max' => 16, 'tooLong' => 'location最长16位'],

            // ['preview', 'default', 'value' => ''],
            ['preview', 'string', 'message' => 'preview必须是字符串',
                                'min' => 2, 'tooShort' => 'preview最短2位',
                                'max' => 512, 'tooLong' => 'preview最长512位'],
            // ['outside', 'default', 'value' => 1],
            ['outside', 'integer', 'message' => 'outside必须为数字，或数字字符串'],
            ['outside', 'in', 'range' => array(1, 2), 'message' => 'outside值必须在1,2中选择'],
            // ['url', 'default', 'value' => ''],
            ['url', 'string', 'message' => 'url必须是字符串',
                                'min' => 2, 'tooShort' => 'url最短2位',
                                'max' => 512, 'tooLong' => 'url最长512位'],
            // ['url_target', 'default', 'value' => 2],
            ['url_target', 'integer', 'message' => 'url_target必须为数字，或数字字符串'],
            ['url_target', 'in', 'range' => array(1, 2), 'message' => 'url_target值必须在1,2中选择'],

            // ['pc_show_count', 'default', 'value' => 0],
            ['pc_show_count', 'integer', 'message' => 'pc_show_count必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'pc_show_count最小存储0', 
                        'max'=>18446744073709551615, 'tooBig' => 'pc_show_count最大存储18446744073709551615'],
            // ['pc_click_count', 'default', 'value' => 0],
            ['pc_click_count', 'integer', 'message' => 'pc_click_count必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'pc_click_count最小存储0', 
                        'max'=>18446744073709551615, 'tooBig' => 'pc_click_count最大存储18446744073709551615'],
            // ['mobile_show_count', 'default', 'value' => 0],
            ['mobile_show_count', 'integer', 'message' => 'mobile_show_count必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'mobile_show_count最小存储0', 
                        'max'=>18446744073709551615, 'tooBig' => 'mobile_show_count最大存储18446744073709551615'],
            // ['mobile_click_count', 'default', 'value' => 0],
            ['mobile_click_count', 'integer', 'message' => 'mobile_click_count必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'mobile_click_count最小存储0', 
                        'max'=>18446744073709551615, 'tooBig' => 'mobile_click_count最大存储18446744073709551615'],

            ['start_time', 'default', 'value' => '1000-01-01 00:00:00', 'on' => 'add'],
            ['start_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'start_time非yyyy-MM-dd HH:mm:ss格式'],
            ['end_time', 'default', 'value' => '1000-01-01 00:00:00', 'on' => 'add'],
            ['end_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'end_time非yyyy-MM-dd HH:mm:ss格式'],
            ['sort', 'integer', 'message' => 'sort必须为数字，或数字字符串', 
                     'min'=>0, 'tooSmall' => 'sort最小存储0', 
                     'max'=>18446744073709551615, 'tooBig' => 'sort最大存储18446744073709551615'],
            ['add_time', 'default', 'value' => '1000-01-01 00:00:00', 'on' => 'add'],
            ['add_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'add_time非yyyy-MM-dd HH:mm:ss格式'],
            // ['display', 'default', 'value' => 1],
            ['display', 'integer', 'message' => 'display必须为数字，或数字字符串'],
            ['display', 'in', 'range' => array(1, 2), 'message' => 'display值必须在1,2中选择'],      
        ];
    }


    /*添加*/
    public function addPoster($data)
    {
        // P($data);changeCatidFormat
        $this->scenario = 'add';
        if(isset($data['Poster']['preview']) and is_array($data['Poster']['preview'])){
            $data['Poster']['preview'] = Tool::changeUploadFormat($data['Poster']['preview']);
        }
        if(isset($data['Poster']['outside'])){
            $data['Poster']['outside'] = Tool::changeBool($data['Poster']['outside']);
        }
        if(isset($data['Poster']['display'])){
            $data['Poster']['display'] = Tool::changeBool($data['Poster']['display']);
        }
        if(!isset($data['Poster']['add_time']) or empty($data['Poster']['add_time'])){
            $data['Poster']['add_time'] = date('Y-m-d H:i:s', time());
        }
        // P($data);
        if($this->load($data) and $this->validate()){
            // P($this);
            if($this->save(false)){
                $id = $this->getPrimaryKey();
                $this->updateAll(['sort' => $id], 'id = :id', [':id' => $id]);
                return $id;
            }
        }
        return false;
    }

    /*
    修改
    $data       修改的数据
    */
    public function modifyPoster($data, $id)
    {
        // P($data);
        $poster = self::find()->where('id = :id', [':id' => $id])->one();
        // P($poster);
        if(isset($data['Poster']['preview']) and is_array($data['Poster']['preview'])){
            $data['Poster']['preview'] = Tool::changeUploadFormat($data['Poster']['preview']);
        }
        if(isset($data['Poster']['outside'])){
            $data['Poster']['outside'] = Tool::changeBool($data['Poster']['outside']);
        }
        if(isset($data['Poster']['display'])){
            $data['Poster']['display'] = Tool::changeBool($data['Poster']['display']);
        }
        // P($data);
        if(!is_null($poster)){
            if($this->load($data) and $this->validate()){
                if(isset($this->name)){
                    $poster->name = $this->name;
                }
                if(isset($this->location)){
                    $poster->location = $this->location;
                }
                if(isset($this->preview)){
                    $poster->preview = $this->preview;
                }
                if(isset($this->outside)){
                    $poster->outside = $this->outside;
                }
                if(isset($this->url_target)){
                    $poster->url_target = $this->url_target;
                }
                if(isset($this->url)){
                    $poster->url = $this->url;
                }
                if(isset($this->display)){
                    $poster->display = $this->display;
                }
                if(isset($this->start_time)){
                    $poster->start_time = $this->start_time;
                }
                if(isset($this->end_time)){
                    $poster->end_time = $this->end_time;
                }
                if($poster->save(false)){
                    return true;
                }
            }
        };
        return false;
    }

    
}